import requests
import json
from PyCmpltrtok.common import md5, sep

PWD = 'my_password_001'


def text_gen(xinput, xusername=''):
    xjson = dict()
    xjson['input'] = xinput
    xjson['check'] = md5(xinput + PWD + xusername)
    xjson['username'] = xusername

    res = requests.post('http://127.0.0.1:7765/api', json=xjson, stream=True)
    
    for chunk in res.iter_lines(decode_unicode=False, delimiter=b'\0'):
        if not chunk:
            continue
        chunk = chunk.decode('utf8')
        data = json.loads(chunk)
        xouput = data['output']
        yield xouput


if '__main__' == __name__:
    sep('1')
    x = '您好，请问今天天气如何？(Fastapi)'
    r = text_gen(x)
    for text in r:
        print(text)

    sep('2')
    x = '请问顺义八维怎么走？(Fastapi)'
    r = text_gen(x)
    for text in r:
        print(text)
    